package com.kyxx.topics.fragment;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kyxx.topics.R;
import com.kyxx.topics.activity.DetailActivity;
import com.kyxx.topics.activity.ZhuanTiActivity;
import com.kyxx.topics.bean.ArticleBean;
import com.kyxx.topics.bean.TagBean;
import com.kyxx.topics.bean.TopicBean;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.ResponseListener;
import com.kyxx.topics.net.RestClient;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.Constants;
import com.kyxx.topics.utils.SizeUtils;
import com.kyxx.topics.utils.TimeUtils;
import com.kyxx.topics.utils.ToastUtils;
import com.kyxx.topics.widget.BannerViewPager;
import com.kyxx.topics.widget.HeaderViewPagerFragment;
import com.loopj.android.http.RequestParams;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.request.BaseRequest;
import com.lzy.widget.HeaderScrollHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.wang.avi.AVLoadingIndicatorView;
import com.youth.banner.Banner;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import okhttp3.Call;
import okhttp3.Response;

public class TopicFragment2 extends HeaderViewPagerFragment {
    private ExpandableListView lv;
    private View topView;
    private Banner banner;
    LinearLayout ll_cotainer;
    BannerViewPager pager_banner;
    int viewCount;
    private boolean isDragging;// 是否被拖拽
    private ImageView mImgCover;
    private int mLastPos;// 记录上一次ViewPager的位置
    private ImageView img_indicator01;
    private RecyclerView rv;
    private Context context;
    private PtrClassicFrameLayout ptrv;
    private RelativeLayout rl_progress;
    private AVLoadingIndicatorView avi;
    private LinearLayout ll_reload;
    private int lastVisiblePosition;
    private TagBean tagBean = new TagBean();
    private List<ArticleBean> articleBeanListBanner = new ArrayList<>();
    private List<ArticleBean> activeBeanListBanner = new ArrayList<>();
    private List<TopicBean> zhuanTiBeanList = new ArrayList<>();
    private TopicAdapter topicAdapter;
    private int pageSize = 1;
    private int countSize = 10;
    private TextView tv_time;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private ExpandableListViewaAdapter expandableListViewaAdapter;
    private DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .showImageOnFail(R.mipmap.image_default)
            .showImageOnLoading(R.mipmap.image_default)
            .showImageForEmptyUri(R.mipmap.image_default)
            .build();
    private DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .showImageOnFail(R.mipmap.icon_user_default)
            .showImageOnLoading(R.mipmap.icon_user_default)
            .showImageForEmptyUri(R.mipmap.icon_user_default)
            .build();
    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
//                    ptrv.setVisibility(View.VISIBLE);
                    rl_progress.setVisibility(View.GONE);
                    break;
                case 1:
                    pager_banner.setCurrentItem(pager_banner.getCurrentItem() + 1);
                    handler.sendEmptyMessageDelayed(1, 3000);
                    break;
                case 2:
                    switch (msg.arg1) {
                        case Constants.INITDATA:
                            rl_progress.setVisibility(View.VISIBLE);
                            avi.setVisibility(View.GONE);
                            ll_reload.setVisibility(View.VISIBLE);
                            break;
                        case Constants.REFRESH:
                            rl_progress.setVisibility(View.VISIBLE);
                            avi.setVisibility(View.GONE);
                            ll_reload.setVisibility(View.VISIBLE);
                            break;
                        default:
                            rl_progress.setVisibility(View.GONE);
                            break;
                    }
                    break;
            }
        }
    };

    @Override
    public View getScrollableView() {
        return lv;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (lv != null) {
            if (isVisibleToUser && lv.getVisibility() == View.VISIBLE) {
                ptrv.autoRefresh();
                initListView(Constants.REFRESH, pageSize, countSize);
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("tag", tagBean);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context = getActivity();
        if (savedInstanceState != null) {
            tagBean = (TagBean) savedInstanceState.get("tag");
        }
        View view = inflater.inflate(R.layout.fragment_topic1, container, false);
        rl_progress = (RelativeLayout) view.findViewById(R.id.rl_progress);
        avi = (AVLoadingIndicatorView) view.findViewById(R.id.avi);
        ll_reload = (LinearLayout) view.findViewById(R.id.ll_reload);
        ll_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageSize = 1;
                initListView(Constants.REFRESH, pageSize, countSize);
                getBannerData();
            }
        });
        lv = (ExpandableListView) view.findViewById(R.id.lv);
        ptrv = (PtrClassicFrameLayout) view.findViewById(R.id.ptrv);
        ptrv.setLastUpdateTimeHeaderRelateObject(context);
        ptrv.setMode(PtrFrameLayout.Mode.REFRESH);
        ptrv.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                ptrv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageSize = 1;
                        initListView(Constants.REFRESH, pageSize, countSize);
                        getBannerData();
                    }
                }, Constants.DELAYTIMES);
            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if (lastVisiblePosition == lv.getLastVisiblePosition())
                    return;
                lastVisiblePosition = lv.getLastVisiblePosition();
                Log.e("topic", lastVisiblePosition + "onScroll" + lv.getCount());
                if (lastVisiblePosition == lv.getCount() - 2 && lv.getCount() >= countSize) {
                    {
                        pageSize++;
                        initListView(Constants.LOADMORE, pageSize, countSize);
                    }
                }
            }
        });
        expandableListViewaAdapter = new ExpandableListViewaAdapter();
        lv.setAdapter(expandableListViewaAdapter);
        initBanner();
        initListView(Constants.INITDATA, pageSize, countSize);
        return view;
    }

    private void initBanner() {
        topView = LayoutInflater.from(context).inflate(R.layout.layout_topic_banner2, null);
        rv = (RecyclerView) topView.findViewById(R.id.rv);
        rv.setFocusable(false);
        ll_cotainer = (LinearLayout) topView.findViewById(R.id.ll_cotainer);
        pager_banner = (BannerViewPager) topView.findViewById(R.id.pager_banner);
        FrameLayout frameLayout = (FrameLayout) topView.findViewById(R.id.frameLayout1);
        pager_banner.setNestParent(frameLayout);
        mImgCover = (ImageView) topView.findViewById(R.id.img_cover);
        img_indicator01 = (ImageView) topView.findViewById(R.id.img_indicator01);
        tv_time = (TextView) topView.findViewById(R.id.tv_time);
        tv_time.setText(TimeUtils.milliseconds2String(System.currentTimeMillis(), new SimpleDateFormat("MM月dd日 E")));
        lv.addHeaderView(topView);
        getBannerData();
    }

    private void getBannerData() {
        HttpParams httpParams = new HttpParams();
        httpParams.put("cat", tagBean.getId());
        String cacheKey = "slidelist&cat=" + tagBean.getId();
        OkGoRequest.post(UrlUtils.SLIDELIST, getActivity(), cacheKey, CacheMode.REQUEST_FAILED_READ_CACHE, httpParams, new StringCallback() {
            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    JSONObject result = new JSONObject(s);
                    handler.removeMessages(1);
                    articleBeanListBanner = JSON.parseArray(result.getJSONArray("data").toString(), ArticleBean.class);
                    if (articleBeanListBanner.size() < 2) {
                        viewCount = 2;
                    } else {
                        viewCount = articleBeanListBanner.size();
                    }
                    if (ll_cotainer.getChildCount() > 1)
                        ll_cotainer.removeViews(1, viewCount - 1);
                    for (int i = 1; i < viewCount; i++) {
                        ImageView imageView = new ImageView(getActivity());
                        imageView.setImageResource(R.mipmap.img_dot);
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.rightMargin = SizeUtils.sp2px(getActivity(), 4);
                        lp.weight = 1;
                        imageView.setLayoutParams(lp);
                        ll_cotainer.addView(imageView);
                    }
                    FragmentManager fm = getChildFragmentManager();
                    MyPagerAdapter adapter = new MyPagerAdapter(fm);
                    pager_banner.setAdapter(adapter);
                    pager_banner.setCurrentItem(articleBeanListBanner.size() * 100);
                    mLastPos = articleBeanListBanner.size() * 100;
                    pager_banner.setOnPageChangeListener(new MyPagerListener());
                    handler.sendEmptyMessageDelayed(1, 3000);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        OkGoRequest.get(UrlUtils.ACLIST, getActivity(), "aclist", CacheMode.REQUEST_FAILED_READ_CACHE, new StringCallback() {
            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    JSONObject result = new JSONObject(s);
                    activeBeanListBanner = JSON.parseArray(result.getJSONArray("data").toString(), ArticleBean.class);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    rv.setLayoutManager(linearLayoutManager);
                    ActiveAdapter activeAdapter = new ActiveAdapter();
                    rv.setAdapter(activeAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            BannerItemFragment fragment = new BannerItemFragment();
            fragment.setResUrl(articleBeanListBanner.get(position % articleBeanListBanner.size()).getImgurl().getPath());
            fragment.setBannerInfo(articleBeanListBanner.get(position % articleBeanListBanner.size()));
            return fragment;
        }

        @Override
        public int getCount() {
            return 10000;
        }

    }

    class MyPagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            int width = mImgCover.getWidth();
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) img_indicator01
                    .getLayoutParams();
            int rightMargin = layoutParams.rightMargin;
            int endPos = (width + rightMargin) * (position % viewCount);
            int startPos = 0;
            if (mLastPos < position) {
                // 图片向右移动
                startPos = (width + rightMargin) * (position % viewCount - 1);
            } else {
                // 图片向左移动
                startPos = (width + rightMargin) * (position % viewCount + 1);
            }
            ObjectAnimator.ofFloat(mImgCover, "translationX", startPos, endPos)
                    .setDuration(300).start();
            mLastPos = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case ViewPager.SCROLL_STATE_DRAGGING:
                    // 用户拖拽
                    isDragging = true;
                    break;
                case ViewPager.SCROLL_STATE_IDLE:
                    // 空闲状态
                    isDragging = false;
                    break;
                case ViewPager.SCROLL_STATE_SETTLING:
                    // 被释放时
                    isDragging = false;
                    break;
                default:
                    break;
            }
        }
    }

    private void initListView(final int type, int page, int count) {
        if (tagBean.getId() == null) {
            return;
        }
        String cacheKey = "specs&cat=" + tagBean.getId() + "&p=" + page + "&rows=" + count;
        OkGoRequest.get(UrlUtils.SPECS + "&cat=" + tagBean.getId() + "&p=" + page + "&rows=" + count, getActivity(), cacheKey, CacheMode.REQUEST_FAILED_READ_CACHE, new StringCallback() {
            @Override
            public void onBefore(BaseRequest request) {
                super.onBefore(request);
                switch (type) {
                    case Constants.INITDATA:
                        rl_progress.setVisibility(View.VISIBLE);
                        avi.setVisibility(View.VISIBLE);
                        ll_reload.setVisibility(View.GONE);
                        break;
                    case Constants.REFRESH:
                        avi.setVisibility(View.VISIBLE);
                        ll_reload.setVisibility(View.GONE);
                        break;
                    default:
                        rl_progress.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onAfter(@Nullable String s, @Nullable Exception e) {
                super.onAfter(s, e);
                ptrv.refreshComplete();
            }

            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onCacheError(Call call, Exception e) {
                super.onCacheError(call, e);
                Message msg = new Message();
                msg.what = 2;
                msg.arg1 = type;
                handler.sendMessageDelayed(msg, Constants.DELAYTIMES);
            }


            @Override
            public void onSuccess(String s, Call call, Response response) {
                handler.sendEmptyMessageDelayed(0, Constants.DELAYTIMES);
                try {
                    JSONObject result = new JSONObject(s);
                    switch (type) {
                        case Constants.INITDATA:
                            zhuanTiBeanList = JSON.parseArray(result.getJSONArray("data").toString(), TopicBean.class);
                            expandableListViewaAdapter = new ExpandableListViewaAdapter();
                            lv.setAdapter(expandableListViewaAdapter);
                            break;
                        case Constants.LOADMORE:
                            zhuanTiBeanList.addAll(JSON.parseArray(result.getJSONArray("data").toString(), TopicBean.class));
                            expandableListViewaAdapter.notifyDataSetChanged();
                            break;
                        case Constants.REFRESH:
                            zhuanTiBeanList = JSON.parseArray(result.getJSONArray("data").toString(), TopicBean.class);
                            expandableListViewaAdapter = new ExpandableListViewaAdapter();
                            lv.setAdapter(expandableListViewaAdapter);
                            break;
                    }
                    lastVisiblePosition = lv.getLastVisiblePosition();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    class TopicAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return zhuanTiBeanList.size();
        }

        @Override
        public Object getItem(int i) {

            return zhuanTiBeanList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.item_topic, null);
                holder.tv_tab = (TextView) view.findViewById(R.id.tv_tab);
                holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
                holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
                holder.tv_content = (TextView) view.findViewById(R.id.tv_content);
                holder.tv_like = (TextView) view.findViewById(R.id.tv_like);
                holder.civ_avtar = (ImageView) view.findViewById(R.id.civ_avtar);
                holder.iv_cover = (ImageView) view.findViewById(R.id.iv_cover);
                holder.line = view.findViewById(R.id.line);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            if (zhuanTiBeanList.get(i).getImgurl() != null) {
                imageLoader.displayImage(UrlUtils.BASEHTTP + zhuanTiBeanList.get(i).getImgurl().getPath(), holder.iv_cover, options);
            } else {
                imageLoader.displayImage("", holder.iv_cover, options);
            }
            holder.tv_title.setText(zhuanTiBeanList.get(i).getTitle());
            holder.tv_content.setText(zhuanTiBeanList.get(i).getDemo());
            holder.tv_tab.setText(zhuanTiBeanList.get(i).getTag());
            return view;
        }
    }

    class ViewHolder {
        private TextView tv_tab;
        private TextView tv_title;
        private TextView tv_name;
        private TextView tv_content;
        private TextView tv_like;
        private ImageView iv_cover;
        private ImageView civ_avtar;
        private RelativeLayout rl_expand;
        private ImageView iv_expand;
        private View line;
    }

    class ActiveAdapter extends RecyclerView.Adapter<ViewHolderActive> {
        @Override
        public ViewHolderActive onCreateViewHolder(ViewGroup parent, int viewType) {
            View view =
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.item_topic_hor_scroll, parent, false);
            ViewHolderActive holder = new ViewHolderActive(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolderActive holder, final int position) {
            if (position == activeBeanListBanner.size() - 1) {
                holder.view_1.setVisibility(View.VISIBLE);
            }
            if (activeBeanListBanner.get(position).getImgurl() != null) {
                imageLoader.displayImage(UrlUtils.BASEHTTP + activeBeanListBanner.get(position).getImgurl().getPath(), holder.iv_cover, options);
            } else {
                imageLoader.displayImage("", holder.iv_cover, options);
            }
            holder.tv_name.setText(activeBeanListBanner.get(position).getTitle());
            holder.iv_cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), DetailActivity.class);
                    intent.putExtra("title", activeBeanListBanner.get(position).getTitle());
                    intent.putExtra("id", activeBeanListBanner.get(position).getId());
                    context.startActivity(intent);
                }
            });

        }

        @Override
        public int getItemCount() {
            return activeBeanListBanner.size();
        }
    }

    public static class ViewHolderActive extends RecyclerView.ViewHolder {
        ImageView iv_cover;
        TextView tv_name;
        View view_1;

        public ViewHolderActive(View view) {
            super(view);
            iv_cover = (ImageView) view.findViewById(R.id.iv_cover);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            view_1 = (View) view.findViewById(R.id.view_1);
        }
    }

    public class ExpandableListViewaAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return zhuanTiBeanList == null ? 0 : zhuanTiBeanList.size();
        }

        @Override
        public int getChildrenCount(int i) {
            return zhuanTiBeanList.get(i).getDoc() == null ? 0 : zhuanTiBeanList.get(i).getDoc().size();
        }

        @Override
        public Object getGroup(int i) {
            return zhuanTiBeanList == null ? null : zhuanTiBeanList.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return zhuanTiBeanList.get(i).getDoc() == null ? null : zhuanTiBeanList.get(i).getDoc().get(i1);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return zhuanTiBeanList.get(i).getDoc() == null ? 0 : i1;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(final int i, final boolean b, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.item_topic, null);
                holder.tv_tab = (TextView) view.findViewById(R.id.tv_tab);
                holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
                holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
                holder.tv_content = (TextView) view.findViewById(R.id.tv_content);
                holder.tv_like = (TextView) view.findViewById(R.id.tv_like);
                holder.civ_avtar = (ImageView) view.findViewById(R.id.civ_avtar);
                holder.iv_cover = (ImageView) view.findViewById(R.id.iv_cover);
                holder.rl_expand = (RelativeLayout) view.findViewById(R.id.rl_expand);
                holder.iv_expand = (ImageView) view.findViewById(R.id.iv_expand);
                holder.line = view.findViewById(R.id.line);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            if (i == 0) {
                holder.line.setVisibility(View.GONE);
            } else {
                holder.line.setVisibility(View.VISIBLE);
            }
            if (b) {
                holder.iv_expand.setImageDrawable(getResources().getDrawable(R.drawable.ic_topic_coll));
            } else {
                holder.iv_expand.setImageDrawable(getResources().getDrawable(R.drawable.ic_topic_expand));
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), ZhuanTiActivity.class);
                    intent.putExtra("title", zhuanTiBeanList.get(i).getTitle());
                    intent.putExtra("demo", zhuanTiBeanList.get(i).getDemo());
                    intent.putExtra("imgUrl", UrlUtils.BASEHTTP + zhuanTiBeanList.get(i).getImgurl().getPath());
                    intent.putExtra("sid", zhuanTiBeanList.get(i).getId());
                    startActivity(intent);
                }
            });
            holder.rl_expand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (b) {
                        lv.collapseGroup(i);
                    } else {
                        lv.expandGroup(i);
                        lv.setSelection(i + 1);
                    }
                }
            });
            if (zhuanTiBeanList.get(i).getImgurl() != null) {
                imageLoader.displayImage(UrlUtils.BASEHTTP + zhuanTiBeanList.get(i).getImgurl().getPath(), holder.iv_cover, options);
            } else {
                imageLoader.displayImage("", holder.iv_cover, options);
            }
            holder.tv_title.setText(zhuanTiBeanList.get(i).getTitle());
            holder.tv_content.setText(zhuanTiBeanList.get(i).getDemo());
            if (TextUtils.isEmpty(zhuanTiBeanList.get(i).getTag())) {
                holder.tv_tab.setText("原创");
            } else {
                holder.tv_tab.setText(zhuanTiBeanList.get(i).getTag());
            }
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            view = View.inflate(context, R.layout.item_zhuanti, null);
            ImageView iv_zhuanti = (ImageView) view.findViewById(R.id.iv_zhuanti);
            TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
            TextView tv_day = (TextView) view.findViewById(R.id.tv_day);
            TextView tv_num = (TextView) view.findViewById(R.id.tv_num);
            ImageView iv_focus = (ImageView) view.findViewById(R.id.iv_focus);
            final TopicBean.DocBean docBean = zhuanTiBeanList.get(i).getDoc().get(i1);
            if (docBean.getImgurl() != null) {
                imageLoader.displayImage(UrlUtils.BASEHTTP + docBean.getImgurl(), iv_zhuanti, options);
            } else {
                imageLoader.displayImage("", iv_zhuanti, options);
            }
            tv_title.setText(docBean.getTitle());
            tv_day.setText(TimeUtils.milliseconds2String(Long.valueOf(docBean.getAddtime()) * 1000, new SimpleDateFormat("MM-dd HH:mm")));
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra("id", docBean.getAid());
                    intent.putExtra("title", docBean.getTitle());
                    startActivity(intent);
                }
            });
//            tv_num.setText(articleBeanList.get(i).getComment());
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }
    }

    public void setValue(TagBean tagBean) {
        this.tagBean = tagBean;
    }

}
